package com.jsgt.greekfire.ui.dialog

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.TextView
import com.fhx.core.utils.DialogUtil
import com.jsgt.greekfire.R
import org.jetbrains.anko.find

class InputDialogUtil {
    companion object {
        private var dialog: Dialog? = null
        fun showInputDialog(
            context: Context,
            title: String,
            hint: String,
            okListener: (content: String) -> Unit
        ) {
//            val contentView =
//                LayoutInflater.from(context).inflate(R.layout.dialog_input, null, false)
//            dialog = DialogUtil.show(context, contentView)
//
//            dialog?.find<TextView>(R.id.txvTitle)?.text = title
//            dialog?.setCanceledOnTouchOutside(true)
//            dialog?.setCancelable(true)
//            dialog?.show()
//            dialog?.setOnDismissListener {
//                dialog = null
//            }
//            dialog?.find<EditText>(R.id.edtContent)?.hint = hint
//            contentView.findViewById<TextView>(R.id.txvCancel).setOnClickListener {
//                dialog?.dismiss()
//            }
//
//            contentView.findViewById<TextView>(R.id.txvOk).setOnClickListener {
//                dialog?.dismiss()
//                val content = contentView.findViewById<EditText>(R.id.edtContent).text.toString().trim()
//                okListener.invoke(content)
//            }

            showInputDialog(context,title,hint,null,okListener)
        }

        fun showInputDialog(
            context: Context,
            title: String,
            hint: String,
            initDialog: ((dialog: Dialog?) -> Unit)?,
            okListener: (content: String) -> Unit
        ) {
            val contentView =
                LayoutInflater.from(context).inflate(R.layout.dialog_input, null, false)
            dialog = DialogUtil.show(context, contentView)

            dialog?.find<TextView>(R.id.txvTitle)?.text = title
            dialog?.setCanceledOnTouchOutside(true)
            dialog?.setCancelable(true)
            dialog?.show()
            dialog?.setOnDismissListener {
                dialog = null
            }
            val editText = dialog?.find<EditText>(R.id.edtContent)
            editText?.hint = hint
            initDialog?.invoke(dialog)
            contentView.findViewById<TextView>(R.id.txvCancel).setOnClickListener {
                dialog?.dismiss()
            }

            contentView.findViewById<TextView>(R.id.txvOk).setOnClickListener {
                dialog?.dismiss()
                val content = contentView.findViewById<EditText>(R.id.edtContent).text.toString().trim()
                okListener.invoke(content)
            }
        }
    }
}